package system.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sun.org.apache.regexp.internal.RE;
import core.exception.AccountingException;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/13.
 */
public class AccountSubjectDao {


    //根据科目名获取科目id
    public String getIdByName(String name){
        Record first = Db.findFirst("select * from account_subject where subject_name = ?", name);
        if(first == null)
            throw new AccountingException("会计科目名称["+name+"]不存在");
        return first.get("subject_id");
    }


    public List getListByType(String subject_type){
        return Db.find("select * from account_subject where subject_type = ?", subject_type);
    }

    public List getType(){
        return Db.query("select DISTINCT subject_type from account_subject");
    }


    public boolean insert(Record accountSubject) {
        return Db.save("account_subject", "subject_id", accountSubject);
    }

    public boolean isExistSubjectIdOrSubjectName(Record accountSubject) {
        return Db.query("select * from account_subject where subject_id = ? or subject_name = ?", accountSubject.get("subject_id"), accountSubject.get("subject_name")).size() > 0;
    }

    public boolean update(Record record) {
        return Db.update("account_subject", "subject_id", record);
    }

    public String getNameById(int id) {
        return Db.queryStr("select subject_name from account_subject where subject_id = ?", id);
    }
}
